#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
	// 分析：前面一组的最大值必须小于等于后面一组的最小值
	// 充分必要条件 每个组的最大值=当前index的时候，就可以分为一组
	/*
	2,1,4,3,5,8,7,6
	4,3,5,1,2,6,7,8
	8,6,1,2,5,3,4,7
	1,2,3,4,5,6,7,8
	
	5,4,3,2,8,7,5
	8,5,4,3,2,7,5
	2,3,4,5,5,7,8
	
	5,4,3,2,1
	1,2,3,4,5
	
	*/
    int maxChunksToSorted(vector<int>& arr) {
		vector<int> st;
		if(arr.size() <= 2) return arr.size();
		int maxN;
		int sum = 0;
		for(int i = 0; i < arr.size(); i++){
			cout << i << endl;
			if(st.empty()){
				st.push_back(st[i]);
				maxN = arr[i];
			}else if(arr[i] < maxN){
				st.push_back(st[i]);
			}else{
				st.clear();
				sum++;
			}
		}
		if(!st.empty()) sum++;
		return sum;
    }
};

int main() {
    Solution so;
	vector <int> nums = {5,4,3,2,1};
    auto a = so.maxChunksToSorted(nums);
    cout << a << endl;
    return 0;
}